import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:permission_handler/permission_handler.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class bank_addController extends BaseController{
  TextEditingController bankNameController = TextEditingController();
  TextEditingController idCardController = TextEditingController();
  TextEditingController bankNumController = TextEditingController();
  TextEditingController bankAddressController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  var bankFontBack = "".obs;

  void reqAddBank() async{
    var bankNum=bankNumController.text;
    var bankAddress=bankAddressController.text;
    if(bankNum.isEmpty){
      SmartDialog.showToast("请输入银行卡卡号");
    }else if(bankAddress.isEmpty){
      SmartDialog.showToast("请输入开户行");
    }else if (bankFontBack.isEmpty) {
      SmartDialog.showToast("请上传身份证正面照");
    }else{
      showLoading();
      var resp =await HttpManager.reqBankAdd(bankAddress,bankNum,bankFontBack.value);
      if(resp.isSuccess()){
        SmartDialog.showNotify(msg: "添加成功", notifyType: NotifyType.success);
        Get.back();
      }
      hideLoading();
    }
  }


  void reqFileUpload(File file) async {
    print('reqFileUpload${file.path}');
    showLoading();
    var newFile = await compressAndGetFile(file);
    print('newFile${newFile.path}');
    var fileResp = await HttpManager.reqFileUpLoad(newFile);
    if (fileResp.isSuccess() && fileResp.data != null) {
      bankFontBack.value=fileResp.data!.fileNameUrl;
    }
    hideLoading();
  }

  final _picker = ImagePicker();

  Future<void> selectImage() async {
    final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
    if (pickedFile != null) {
      // 处理所选照片
      reqFileUpload(File(pickedFile.path));
    }
    Get.back();
  }

  Future<void> selectCamera() async {
    //验证权限是否授权
    var status = await Permission.camera.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      final pickedFile = await _picker.pickImage(source: ImageSource.camera);
      if (pickedFile != null) {
        // 处理所选照片
        reqFileUpload(File(pickedFile.path));
      }
    } else {
      //最好是在调用openAppSettings();之前加一个弹窗说明用户没有对应权限需要去设置页面开启
      openAppSettings();
    }
    Get.back();
  }

  /// 图片压缩 File -> File
  Future<File> compressAndGetFile(File file) async {
    final dir = await path_provider.getTemporaryDirectory();
    await dir.create(recursive: true);
    final targetPath =
        dir.absolute.path + '/${DateTime.now().microsecondsSinceEpoch}.jpg';
    var result = await FlutterImageCompress.compressAndGetFile(
      file.path,
      targetPath,
      quality: 88,
      rotate: 0,
    );
    return File(result!.path);
  }

  File createFile(String path) {
    final file = File(path);
    if (!file.existsSync()) {
      file.createSync(recursive: true);
    }
    return file;
  }
}
